package com.alibaba.citrus.cr.mn.order.center.facade.function.impl;

import cn.hutool.core.date.StopWatch;
import com.alibaba.citrus.cr.common.enums.GeneralStatusEnum;
import com.alibaba.citrus.cr.common.enums.ordercenter.*;
import com.alibaba.citrus.cr.common.exception.FunctionException;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.citrus.cr.mn.order.center.common.SetOrderTag;
import com.alibaba.citrus.cr.mn.order.center.common.util.AssertUtils;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.BaseDataServiceAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.DictionaryVerifyAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.function.common.OrderAtomQueryService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.AtomOrderDOToSalesOrderAtomQueryResponseConvertor;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.BeanConverter;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.PageQuerySaleOrderDetailCovert;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.SaleOrderIndexToSalesOrderAtomQueryResponseConvertor;
import com.alibaba.citrus.cr.mn.order.center.facade.function.dataobject.AtomOrderDO;
import com.alibaba.citrus.cr.mn.order.center.facade.repository.AtomOrderQueryRepository;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crb2btradecenter.domain.order.dto.QueryByOrderNosRequest;
import com.epoch.app.crb2btradecenter.model.dto.PageQueryOrderEsListRequest4OrderCenter;
import com.epoch.app.crb2btradecenter.model.dto.PageQuerySaleOrderDetailGroupRequest;
import com.epoch.app.crb2btradecenter.obtcsaleorderindex.model.ObtcSaleOrderIndex;
import com.epoch.app.crb2btradecenter.obtcsaleorderindex.service.ObtcSaleOrderIndexService;
import com.epoch.app.crb2btradecenter.obtcsaleorderlineindex.model.ObtcSaleOrderLineIndex;
import com.epoch.app.crb2btradecenter.obtcsaleorderlineindex.service.ObtcSaleOrderLineIndexService;
import com.epoch.app.mnordercenter.contants.MnOrderCenterConstants;
import com.epoch.app.mnordercenter.model.dto.DictionaryResponse;
import com.epoch.app.mnordercenter.model.dto.SalesOrderAtomPageQueryRequest;
import com.epoch.app.mnordercenter.model.dto.SalesOrderAtomQueryListRequest;
import com.epoch.app.mnordercenter.model.dto.SalesOrderAtomQueryResponse;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author zhangxiaorui
 * @ClassName SalesOrderAtomQueryServiceImpl
 * @description: TODO
 * @date 2023/04/23 16:45
 * @version: 1.0
 */
@Service("salesOrderAtomQueryService")
public class SalesOrderAtomQueryServiceImpl implements OrderAtomQueryService<SalesOrderAtomQueryListRequest, SalesOrderAtomPageQueryRequest, SalesOrderAtomQueryResponse> {
    private static final Log log = Log.getLogger(SalesOrderAtomQueryServiceImpl.class);

    @Resource
    private AtomOrderQueryRepository atomOrderQueryRepository;
    @Resource
    private BaseDataServiceAdapter baseDataServiceAdapter;
    @Resource
    private ObtcSaleOrderIndexService obtcSaleOrderIndexService;
    @Resource
    private ObtcSaleOrderLineIndexService obtcSaleOrderLineIndexService;
    @Resource
    private DictionaryVerifyAdapter dictionaryVerifyAdapter;

    @Override
    public PageResult<List<SalesOrderAtomQueryResponse>> listPage(SalesOrderAtomQueryListRequest queryRequest) {
        return listPage(queryRequest, OrderCategoryEnum.COMPANY.getCode());
    }

    @Override
    public PageResult<List<SalesOrderAtomQueryResponse>> listPage(SalesOrderAtomQueryListRequest queryRequest, String orderCategory) {
        filterBlankOrderNo(queryRequest);
        if (queryRequest.getPage() == null) {
            queryRequest.setPage(1);
        }
        if (queryRequest.getSize() == null) {
            queryRequest.setSize(10);
        }
        paramsCheck(queryRequest, orderCategory);

        // TODO 交货单查询预留, 目前传了交货单直接返回空
        if (CollectionUtils.isNotEmpty(queryRequest.getDeliveryOrderNo())) {
            return PageResult.of(Lists.newArrayList(), 0);
        }

        QueryByOrderNosRequest request = QueryByOrderNosRequest.builder().build();
        BeanConverter.convert(queryRequest, request);
        request.setSalesOrderNo(queryRequest.getSalesOrderNo());
        //改用setBusinessUnitOutCode查 因6255会查出来2个businessUnitcode
        request.setPreOrderNos(queryRequest.getPreOrderNo());
        request.setBusinessUnitOutCode(queryRequest.getBusinessUnitCode());
        request.setBusinessUnitCode(null);
        request.setOrderTag(SetOrderTag.NORMAL);

        Result<List<AtomOrderDO>> listResult = atomOrderQueryRepository.queryByOrderNos(request);
        log.info("Order_listPage_Request={}，size={}", JSONObject.toJSON(request), listResult.getTotal());

        if (CollectionUtils.isEmpty(listResult.getResult())) {
            return PageResult.of(Lists.newArrayList(), listResult.getTotal());
        }
        List<SalesOrderAtomQueryResponse> result =
                AtomOrderDOToSalesOrderAtomQueryResponseConvertor.convert(listResult.getResult());
        log.info("result销售订单结果:{}",JSON.toJSONString(result));
        List<DictionaryResponse> businessTypeResponseList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.BUSINESS_TYPE.getCode());
        Map<String, DictionaryResponse> businessTypeMap = businessTypeResponseList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
        for (SalesOrderAtomQueryResponse salesOrder : result) {
            if (salesOrder.getBusinessType() != null && businessTypeMap.containsKey(salesOrder.getBusinessType().toString())) {
                salesOrder.setBusinessTypeName(businessTypeMap.get(salesOrder.getBusinessType().toString()).getName());
            }
        }
        return PageResult.of(result, listResult.getTotal());
    }

    @Override
    public PageResult<List<SalesOrderAtomQueryResponse>> page(SalesOrderAtomPageQueryRequest request, String orderTag) {
        return page(request, orderTag, OrderCategoryEnum.COMPANY.getCode());
    }

    @Override
    public PageResult<List<SalesOrderAtomQueryResponse>> page(SalesOrderAtomPageQueryRequest request, String orderTag, String orderCategoryCode){
        if (request.getPage() == null) {
            request.setPage(1);
        }
        if (request.getSize() == null) {
            request.setSize(10);
        }
        salesOrderAtomPageQueryParamCheck(request, orderCategoryCode);
        return pageQueryByEs(request, orderTag);
    }

    private void salesOrderAtomPageQueryParamCheck(SalesOrderAtomPageQueryRequest request, String orderCategoryCode) {
        baseParamsCheck(request.getFromSys(), request.getBusinessUnitCode(), request.getBusinessType(), request.getPage(), request.getSize());

        AssertUtils.lengthCheck(request.getShipToPartyCode(), 50, false, "OC-01-001-01-15-004", "客户送达方编码");
        AssertUtils.lengthCheck(request.getCustomerCode(), 50, false, "OC-01-001-01-15-004", "客户售达方编码");
        AssertUtils.lengthCheck(request.getSalesOrganizationCode(), 50, false, "OC-01-001-01-15-004", "销售组织编码");
        AssertUtils.lengthCheck(request.getProductGroupCode(), 50, false, "OC-01-001-01-15-004", "产品组编码");
        AssertUtils.lengthCheck(request.getPreOrderNo(), 50, false, "OC-01-001-01-15-004", "预报订单号");
        AssertUtils.lengthCheck(request.getNote(), 255, false, "OC-01-001-01-15-004", "备注（主单）");
        AssertUtils.lengthCheck(request.getCreatorCode(), 50, false, "OC-01-001-01-15-004", "创建人工号");
        AssertUtils.lengthCheck(request.getModifierCode(), 50, false, "OC-01-001-01-15-004", "修改人工号");
        AssertUtils.lengthCheck(request.getCustomerPurchaseOrderNo(), 50, false, "OC-01-001-01-15-004", "客户采购订单号");
        AssertUtils.lengthCheck(request.getDeliveryFactoryCode(), 50, false, "OC-01-001-01-15-004", "发货工厂编码");
        AssertUtils.lengthCheck(request.getStockCode(), 50, false, "OC-01-001-01-15-004", "发货库存地点编码");
        AssertUtils.lengthCheck(request.getContact(), 50, false, "OC-01-001-01-15-004", "联系人");
        AssertUtils.lengthCheck(request.getContactPhone(), 50, false, "OC-01-001-01-15-004", "联系电话");
        AssertUtils.lengthCheck(request.getSalesOrderNo(), 50, false, "OC-01-001-01-15-004", "销售订单号");
        AssertUtils.lengthCheck(request.getBusinessChannelCode(), 50, false, "OC-01-001-01-15-004", "业务渠道编码");
        AssertUtils.lengthCheck(request.getCustomerGrade(), 50, false, "OC-01-001-01-15-004", "客户等级");
        AssertUtils.lengthCheck(request.getCustomerGroup(), 50, false, "OC-01-001-01-15-004", "客户组");
        AssertUtils.lengthCheck(request.getFulfillmentTypeCode(), 50, false, "OC-01-001-01-15-004", "履约方式");
        AssertUtils.lengthCheck(request.getFulfillmentPriority(), 10, false, "OC-01-001-01-15-004", "履约优先级");
        AssertUtils.lengthCheck(request.getReceiveStatus(), 50, false, "OC-01-001-01-15-004", "收货状态");
        AssertUtils.lengthCheckList(request.getOutOrderNoList(), "外部单号集合", "外部单号", 20, 50);
        AssertUtils.lengthCheckList(request.getKaSystemList(), "KA系统集合", "KA系统", 10, 50);
        AssertUtils.lengthCheckList(request.getKaAreaList(), "KA大区集合", "KA大区", 10, 50);
        if (StringUtils.isNotEmpty(request.getOrderBizTag())) {
            AssertUtils.isTrue(request.getOrderBizTag().length() > 50, "OC-01-001-01-15-004", "订单标签", request.getOrderBizTag().length(), "50");
            AssertUtils.isTrue(!OrderBizTagEnum.codes().contains(request.getOrderBizTag()), "OC-01-001-01-15-006", "订单标签");
        }
        if (CollectionUtils.isNotEmpty(request.getSalesOrderNoList())) {
            AssertUtils.isTrue(request.getSalesOrderNoList().size() > 50, "OC-01-001-01-15-004", "销售订单号集合", request.getSalesOrderNoList().size(), 50);
            checkItemLength(request.getSalesOrderNoList(), 50, "销售订单号");
        }
        if (CollectionUtils.isNotEmpty(request.getPreOrderNoList())) {
            AssertUtils.isTrue(request.getPreOrderNoList().size() > 50, "OC-01-001-01-15-004", "预报订单号集合", request.getPreOrderNoList().size(), 50);
            checkItemLength(request.getPreOrderNoList(), 50, "预报订单号");
        }
        if (OrderCategoryEnum.GROUP.getCode().equals(orderCategoryCode) && CollectionUtils.isNotEmpty(request.getSalesOrderType())) {
            AssertUtils.isTrue(request.getSalesOrderType().size() > 10, "OC-01-001-01-15-004", "订单类型编码集合", request.getSalesOrderType().size(), 10);
            checkItemLength(request.getSalesOrderType(), 10, "订单类型编码");
            List<String> checkExistList = request.getSalesOrderType().stream().filter(x ->
                    !Lists.newArrayList(SalesOrderTypeEnum.GROUP_DELIVERY_ORDER.getCode(),
                            SalesOrderTypeEnum.GROUP_FREE_ORDER.getCode(),
                            SalesOrderTypeEnum.GROUP_STD_ORDER.getCode()).contains(x)).collect(Collectors.toList());
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "订单类型编码集合");
        }
        if (CollectionUtils.isNotEmpty(request.getBusinessUnitCodeList())) {
            AssertUtils.isTrue(request.getBusinessUnitCodeList().size() > 10, "OC-01-001-01-15-004", "业务单元编码集合", request.getBusinessUnitCodeList().size(), "10");
            checkItemLength(request.getBusinessUnitCodeList(), 50, "业务单元编码");
            request.getBusinessUnitCodeList().forEach(businessUnitCode -> {
                dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_UNIT_CODE.getCode(), businessUnitCode, new FunctionException("OC-01-001-01-15-006", "业务单元编码集合"));
            });
        }
        if (CollectionUtils.isNotEmpty(request.getOrderChannelCodeList())) {
            AssertUtils.isTrue(request.getOrderChannelCodeList().size() > 10, "OC-01-001-01-15-004", "订单渠道编码集合", request.getOrderChannelCodeList().size(), "10");
            checkItemLength(request.getOrderChannelCodeList(), 50, "订单渠道编码");
            List<String> checkExistList = request.getOrderChannelCodeList().stream().filter(x -> !OrderChannelEnum.codes().contains(x)).collect(Collectors.toList());
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "订单渠道编码集合");
        }
        if(CollectionUtils.isNotEmpty(request.getOrderReasonCodeList())){
            AssertUtils.isTrue(request.getOrderReasonCodeList().size() > 10, "OC-01-001-01-15-004", "订单原因编码集合", request.getOrderReasonCodeList().size(), 10);
            checkItemLength(request.getOrderReasonCodeList(), 50, "订单原因编码");
            List<String> checkExistList = request.getOrderReasonCodeList().stream().filter(x -> !SalesOrderReasonEnum.codes().contains(x)).collect(Collectors.toList());
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "订单原因编码集合");
        }
        if(CollectionUtils.isNotEmpty(request.getCloseReasonCodeList())){
            AssertUtils.isTrue(request.getCloseReasonCodeList().size() > 10, "OC-01-001-01-15-004", "关闭原因编码集合", request.getCloseReasonCodeList().size(), 10);
            checkItemLength(request.getCloseReasonCodeList(), 50, "关闭原因编码");
            List<String> checkExistList = request.getCloseReasonCodeList().stream().filter(x -> !SalesOrderCloseEnum.codes().contains(x)).collect(Collectors.toList());
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "关闭原因编码集合");
        }
        // 履约方式枚举校验
        if (StringUtils.isNotBlank(request.getFulfillmentTypeCode())) {
            AssertUtils.isTrue(request.getFulfillmentTypeCode().length() > 50, "OC-01-001-01-15-004", "履约方式", request.getFulfillmentTypeCode().length(), "50");
            String fulfillmentTypeCode = DictionaryTypeEnum.COMPANY_SALE_FULFILLMENT_TYPE.getCode();
            if (OrderCategoryEnum.GROUP.getCode().equals(orderCategoryCode)) {
                fulfillmentTypeCode = DictionaryTypeEnum.GROUP_SALE_FULFILLMENT_TYPE.getCode();
            }
            List<DictionaryResponse> fulfillmentTypeList = baseDataServiceAdapter.queryDictionarys(fulfillmentTypeCode);
            Map<String, DictionaryResponse> fulfillmentTypeMap = fulfillmentTypeList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
            AssertUtils.isTrue(fulfillmentTypeMap.get(request.getFulfillmentTypeCode()) == null, "OC-01-001-01-15-006", "履约方式编码");
        }
        // 履约优先级枚举校验
        if (StringUtils.isNotBlank(request.getFulfillmentPriority())) {
            AssertUtils.isTrue(request.getFulfillmentPriority().length() > 10, "OC-01-001-01-15-004", "履约优先级", request.getFulfillmentPriority().length(), "10");
            FulfillmentPriorityEnum fulfillmentPriorityEnum = FulfillmentPriorityEnum.of(request.getFulfillmentPriority());
            AssertUtils.isTrue(fulfillmentPriorityEnum == null, "OC-01-001-01-15-006", "履约优先级");
        }
        if (CollectionUtils.isNotEmpty(request.getSalesDepartmentCode())) {
            AssertUtils.isTrue(request.getSalesDepartmentCode().size() > 10, "OC-01-001-01-15-004", "销售部门编码集合", request.getSalesDepartmentCode().size(), 10);
            checkItemLength(request.getSalesDepartmentCode(), 50, "销售部门编码");
        }
        if (CollectionUtils.isNotEmpty(request.getSalesChannelCode())) {
            AssertUtils.isTrue(request.getSalesChannelCode().size() > 10, "OC-01-001-01-15-004", "销售渠道编码集合", request.getSalesChannelCode().size(), 10);
            checkItemLength(request.getSalesChannelCode(), 50, "销售渠道编码");
        }
        if (CollectionUtils.isNotEmpty(request.getFromSysList())) {
            AssertUtils.isTrue(request.getFromSysList().size() > 10, "OC-01-001-01-15-004", "来源系统编码集合", request.getFromSysList().size(), 10);
            checkItemLength(request.getFromSysList(), 50, "来源系统编码");
        }
        if (CollectionUtils.isNotEmpty(request.getSourceOrderNoList())) {
            AssertUtils.isTrue(request.getSourceOrderNoList().size() > 50, "OC-01-001-01-15-004", "来源单号集合", request.getSourceOrderNoList().size(), 50);
            checkItemLength(request.getSourceOrderNoList(), 50, "来源单号");
        }
        if (CollectionUtils.isNotEmpty(request.getStatusList())) {
            AssertUtils.isTrue(request.getStatusList().size() > 10, "OC-01-001-01-15-004", "单据状态集合", request.getStatusList().size(), "10");
            checkItemLength(request.getStatusList(), 10, "单据状态");
            List<String> statusList = SalesOrderGroupStatusEnum.codes().stream().map(String::valueOf).collect(Collectors.toList());
            List<String> checkExistList = request.getStatusList().stream().filter(x -> !statusList.contains(x)).collect(Collectors.toList());
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "单据状态集合");
        }
        if (CollectionUtils.isNotEmpty(request.getReceiveStatusList())) {
            AssertUtils.isTrue(request.getReceiveStatusList().size() > 10, "OC-01-001-01-15-004", "单据收货状态集合", request.getReceiveStatusList().size(), "10");
            checkItemLength(request.getReceiveStatusList(), 10, "单据收货状态");
            List<String> statusList = SalesOrderGroupReceiveStatusEnum.codes().stream().map(String::valueOf).collect(Collectors.toList());
            List<String> checkExistList = request.getReceiveStatusList().stream().filter(x -> !statusList.contains(x)).collect(Collectors.toList());
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "单据收货状态集合");
        }
        if (CollectionUtils.isNotEmpty(request.getDeliveryMethodCodeList())) {
            AssertUtils.isTrue(request.getDeliveryMethodCodeList().size() > 10, "OC-01-001-01-15-004", "配送方式编码集合", request.getDeliveryMethodCodeList().size(), 10);
            checkItemLength(request.getDeliveryMethodCodeList(), 50, "配送方式编码");
            List<String> checkExistList = request.getDeliveryMethodCodeList().stream().filter(x -> !SalesOrderDeliveryMethodEnum.codes().contains(x)).collect(Collectors.toList());
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "配送方式编码集合");
        }
        if (CollectionUtils.isNotEmpty(request.getReceiveWarehouseCodeList())) {
            AssertUtils.isTrue(request.getReceiveWarehouseCodeList().size() > 10, "OC-01-001-01-15-004", "收货仓（逻辑仓库编码）集合", request.getReceiveWarehouseCodeList().size(), 10);
            checkItemLength(request.getReceiveWarehouseCodeList(), 50, "收货仓（逻辑仓库编码）");
        }
        if (CollectionUtils.isNotEmpty(request.getShippingWarehouseCodeList())) {
            AssertUtils.isTrue(request.getShippingWarehouseCodeList().size() > 10, "OC-01-001-01-15-004", "发货仓（逻辑仓库编码）集合", request.getShippingWarehouseCodeList().size(), 10);
            checkItemLength(request.getShippingWarehouseCodeList(), 50, "发货仓（逻辑仓库编码）");
        }
        if (CollectionUtils.isNotEmpty(request.getCloseReasonCodeList())) {
            AssertUtils.isTrue(request.getCloseReasonCodeList().size() > 10, "OC-01-001-01-15-004", "关闭原因编码集合", request.getCloseReasonCodeList().size(), 10);
            checkItemLength(request.getCloseReasonCodeList(), 50, "关闭原因编码");
        }
        if (CollectionUtils.isNotEmpty(request.getProductCodeList())) {
            AssertUtils.isTrue(request.getProductCodeList().size() > 10, "OC-01-001-01-15-004", "物料编码集合", request.getProductCodeList().size(), 10);
            checkItemLength(request.getProductCodeList(), 50, "物料编码");
        }

        // 时间格式确认
        Map<String, String> vaildBeginTimeMap = AssertUtils.timeFormatCheck(request.getVaildBeginTime(), request.getValidEndTime(), "生效日期（开始日期）", "生效日期（结束日期）", AssertUtils.TYPE_DATE);
        Map<String, String> expectedDeliveryTimeMap = AssertUtils.timeFormatCheck(request.getExpectedDeliveryBeginDate(), request.getExpectedDeliveryEndDate(), "期望交货日期（开始日期）", "期望交货日期（结束日期）", AssertUtils.TYPE_DATE);
        Map<String, String> createTimeMap = AssertUtils.timeFormatCheck(request.getCreateBeginTime(), request.getCreateEndTime(), "创建时间（开始时间）", "创建时间（结束时间）", AssertUtils.TYPE_TIME);
        Map<String, String> updateTimeMap = AssertUtils.timeFormatCheck(request.getModifyBeginTime(), request.getModifyEndTime(), "修改时间（开始时间）", "修改时间（结束时间）", AssertUtils.TYPE_TIME);
        Map<String, String> sourceTimeMap = AssertUtils.timeFormatCheck(request.getSourceBeginTime(), request.getSourceEndTime(), "来单时间（开始时间）", "来单时间（结束时间）", AssertUtils.TYPE_TIME);
        request.setVaildBeginTime(vaildBeginTimeMap.get(AssertUtils.START_TIME));
        request.setValidEndTime(vaildBeginTimeMap.get(AssertUtils.END_TIME));
        request.setExpectedDeliveryBeginDate(expectedDeliveryTimeMap.get(AssertUtils.START_TIME));
        request.setExpectedDeliveryEndDate(expectedDeliveryTimeMap.get(AssertUtils.END_TIME));
        request.setCreateBeginTime(createTimeMap.get(AssertUtils.START_TIME));
        request.setCreateEndTime(createTimeMap.get(AssertUtils.END_TIME));
        request.setModifyBeginTime(updateTimeMap.get(AssertUtils.START_TIME));
        request.setModifyEndTime(updateTimeMap.get(AssertUtils.END_TIME));
        request.setSourceBeginTime(sourceTimeMap.get(AssertUtils.END_TIME));
        request.setSourceEndTime(sourceTimeMap.get(AssertUtils.END_TIME));
        if(CollectionUtils.isNotEmpty(request.getFromSysList())) {
            request.getFromSysList().forEach(x -> {
                dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.FROM_SYS.getCode(), x, new FunctionException("OC-01-001-01-15-006", "来源系统编码"));
            });
        }
        AssertUtils.isWholeNumber(request.getContactPhone(), false, "OC-01-001-01-15-006", "联系电话");
    }

    @Override
    public PageResult<List<SalesOrderAtomQueryResponse>> listPageForInner(SalesOrderAtomQueryListRequest queryRequest) {
        log.info("Order_listPageForInner_Request request={}", JSONObject.toJSON(queryRequest));

        filterBlankOrderNo(queryRequest);
        if (queryRequest.getPage() == null) {
            queryRequest.setPage(1);
        }
        if (queryRequest.getSize() == null) {
            queryRequest.setSize(10);
        }
        paramsCheck(queryRequest, null);

        // TODO 交货单查询预留, 目前传了交货单直接返回空
        if (CollectionUtils.isNotEmpty(queryRequest.getDeliveryOrderNo())) {
            return PageResult.of(Lists.newArrayList(), 0);
        }

        QueryByOrderNosRequest request = QueryByOrderNosRequest.builder().build();
        BeanConverter.convert(queryRequest, request);
        request.setSalesOrderNo(queryRequest.getSalesOrderNo());
        //改用setBusinessUnitOutCode查 因6255会查出来2个businessUnitcode
        request.setPreOrderNos(queryRequest.getPreOrderNo());
        request.setBusinessUnitOutCode(queryRequest.getBusinessUnitCode());
        request.setBusinessUnitCode(null);
        request.setOrderTag(SetOrderTag.NORMAL);

        Result<List<AtomOrderDO>> listResult = atomOrderQueryRepository.queryByOrderNos(request);
        log.info("Order_listPage_Request={}，size={}", JSONObject.toJSON(request), listResult.getTotal());

        if (CollectionUtils.isEmpty(listResult.getResult())) {
            return PageResult.of(Lists.newArrayList(), listResult.getTotal());
        }
        List<SalesOrderAtomQueryResponse> result =
                AtomOrderDOToSalesOrderAtomQueryResponseConvertor.convertForInner(listResult.getResult());
        List<DictionaryResponse> businessTypeResponseList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.BUSINESS_TYPE.getCode());
        Map<String, DictionaryResponse> businessTypeMap = businessTypeResponseList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
        for (SalesOrderAtomQueryResponse salesOrder : result) {
            if (salesOrder.getBusinessType() != null && businessTypeMap.containsKey(salesOrder.getBusinessType().toString())) {
                salesOrder.setBusinessTypeName(businessTypeMap.get(salesOrder.getBusinessType().toString()).getName());
            }
        }
        return PageResult.of(result, listResult.getTotal());
    }

    @Override
    public Result<List<ObtcSaleOrderLineIndex>> detailListPage(SalesOrderAtomPageQueryRequest request) {
        return detailListPage(request, OrderCategoryEnum.COMPANY.getCode());
    }

    @Override
    public Result<List<ObtcSaleOrderLineIndex>> detailListPage(SalesOrderAtomPageQueryRequest request, String orderCategoryCode) {
        if (request.getPage() == null) {
            request.setPage(1);
        }
        if (request.getSize() == null) {
            request.setSize(10);
        }
        // 数据校验
        paramsCheck(request, orderCategoryCode);
        // 查询
        return pageQueryByEs(request);
    }

    @Override
    public Result<List<ObtcSaleOrderLineIndex>> detailListPageInner(SalesOrderAtomPageQueryRequest request) {
        if (request.getPage() == null) {
            request.setPage(1);
        }
        if (request.getSize() == null) {
            request.setSize(10);
        }
        // 数据校验
        paramsCheckInner(request);
        // 查询
        return pageQueryByEs(request);
    }

    @Override
    public Result<List<ObtcSaleOrderLineIndex>> pageOrderDetail(SalesOrderAtomPageQueryRequest request) {
        if (request.getPage() == null) {
            request.setPage(1);
        }
        if (request.getSize() == null) {
            request.setSize(10);
        }
        Map<String, String> createTimeMap = AssertUtils.timeFormatCheck(request.getCreateBeginTime(), request.getCreateEndTime(), "创建时间（开始时间）", "创建时间（结束时间）", AssertUtils.TYPE_TIME);
        request.setCreateBeginTime(createTimeMap.get(AssertUtils.START_TIME));
        request.setCreateEndTime(createTimeMap.get(AssertUtils.END_TIME));
        return pageQueryByEs(request);
    }

    private void paramsCheckInner(SalesOrderAtomPageQueryRequest request) {
        AssertUtils.isTrue(StringUtils.isBlank(request.getOrderCategoryCode()), "OC-01-001-01-15-001", "订单类别编码");
        AssertUtils.isTrue(CollectionUtils.isEmpty(request.getSalesOrderTypeList()), "OC-01-001-01-15-001", "订单类型编码集合");
        AssertUtils.isTrue(CollectionUtils.isEmpty(request.getFulfillmentStatusList()), "OC-01-001-01-15-001", "履约状态集合");

        List<String> checkExistList = null;
        if (OrderCategoryEnum.GROUP.getCode().equals(request.getOrderCategoryCode())) {
            checkExistList = request.getSalesOrderTypeList().stream().filter(x ->
                    !Lists.newArrayList(SalesOrderTypeEnum.GROUP_DELIVERY_ORDER.getCode(),
                            SalesOrderTypeEnum.GROUP_FREE_ORDER.getCode(),
                            SalesOrderTypeEnum.GROUP_STD_ORDER.getCode()).contains(x)).collect(Collectors.toList());
        } else {
            checkExistList = request.getSalesOrderTypeList().stream().filter(x ->
                    !SalesOrderTypeEnum.companyOrderCodes().contains(x)).collect(Collectors.toList());
        }
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "订单类型编码集合");
        Map<String, String> expectedDeliveryTimeMap = AssertUtils.timeFormatCheck(request.getExpectedDeliveryBeginDate(), request.getExpectedDeliveryEndDate(), "期望交货日期（开始日期）", "期望交货日期（结束日期）", AssertUtils.TYPE_DATE);
        request.setExpectedDeliveryBeginDate(expectedDeliveryTimeMap.get(AssertUtils.START_TIME));
        request.setExpectedDeliveryEndDate(expectedDeliveryTimeMap.get(AssertUtils.END_TIME));
        if (CollectionUtils.isNotEmpty(request.getFulfillmentStatusList())) {
            AssertUtils.isTrue(request.getFulfillmentStatusList().stream().anyMatch(fulfillmentStatus -> Objects.isNull(FulfillmentStatusEnum.of(fulfillmentStatus))), "OC-01-001-01-15-006", "履约状态");
        }
        Map<String, String> createTimeMap = AssertUtils.timeFormatCheck(request.getCreateBeginTime(), request.getCreateEndTime(), "创建时间（开始时间）", "创建时间（结束时间）", AssertUtils.TYPE_TIME);
        request.setCreateBeginTime(createTimeMap.get(AssertUtils.START_TIME));
        request.setCreateEndTime(createTimeMap.get(AssertUtils.END_TIME));
    }


    private Result<List<ObtcSaleOrderLineIndex>> pageQueryByEs(SalesOrderAtomPageQueryRequest salesOrderGroupPageQueryRequest) {
        PageQuerySaleOrderDetailGroupRequest esRequest = PageQuerySaleOrderDetailCovert.covert(salesOrderGroupPageQueryRequest);
        log.info("SalesOrderAtomQueryServiceImpl#pageQueryByEs esRequest={}", JSON.toJSONString(esRequest));

        Result<List<ObtcSaleOrderLineIndex>> listResult = obtcSaleOrderLineIndexService.pageOrderLineList(esRequest);
        log.info("SalesOrderAtomQueryServiceImpl#pageQueryByEs listResult={}", JSON.toJSONString(listResult));

        if (CollectionUtils.isNotEmpty(listResult.getResult())) {
            listResult.setSuccess(true);
        }
        return listResult;
    }

    private void paramsCheck(SalesOrderAtomPageQueryRequest request, String orderCategoryCode) {
        // 必填项校验
        AssertUtils.isTrue(StringUtils.isBlank(request.getBusinessType()), "OC-01-001-01-15-001", "业态编码");
        AssertUtils.lengthCheck(request.getBusinessType(), 10, true, "OC-01-001-01-15-004", "业态编码");
        AssertUtils.lengthCheck(request.getFromSys(), 50, false, "OC-01-001-01-15-004", "来源系统编码");
        AssertUtils.lengthCheck(request.getBusinessUnitCode(), 50, false, "OC-01-001-01-15-004", "业务单元编码");
        AssertUtils.lengthCheck(request.getOrderCategoryCode(), 50, false, "OC-01-001-01-15-004", "订单类别编码");
        AssertUtils.lengthCheck(request.getSalesOrderNo(), 50, false, "OC-01-001-01-15-004", "销售订单号");
        AssertUtils.lengthCheck(request.getPreOrderNo(), 50, false, "OC-01-001-01-15-004", "预报订单号");

        AssertUtils.lengthCheck(request.getOrderChannelCode(), 50, false, "OC-01-001-01-15-004", "订单渠道编码");
        AssertUtils.lengthCheck(request.getSourceOrderNo(), 50, false, "OC-01-001-01-15-004", "来源单号");
        AssertUtils.lengthCheck(request.getOutOrderNo(), 50, false, "OC-01-001-01-15-004", "外部单号");
        AssertUtils.lengthCheckList(request.getKaSystemList(), "KA系统集合", "KA系统", 10, 50);
        AssertUtils.lengthCheckList(request.getKaAreaList(), "KA大区集合", "KA大区", 10, 50);
        AssertUtils.lengthCheckList(request.getOutOrderNoList(), "外部单号集合", "外部单号", 20, 50);

        // 长度校验
        if (StringUtils.isNotBlank(request.getProductGroupCode())) {
            AssertUtils.isTrue(request.getProductGroupCode().length() > 50, "OC-01-001-01-15-004", "产品组编码", request.getProductGroupCode().length(), "50");
        }
        if (StringUtils.isNotBlank(request.getBusinessChannelCode())) {
            AssertUtils.isTrue(request.getBusinessChannelCode().length() > 50, "OC-01-001-01-15-004", "业务渠道编码", request.getBusinessChannelCode().length(), "50");
        }
        if (StringUtils.isNotBlank(request.getSalesOrganizationCode())) {
            AssertUtils.isTrue(request.getSalesOrganizationCode().length() > 50, "OC-01-001-01-15-004", "销售组织编码", request.getSalesOrganizationCode().length(), "50");
        }
        if (StringUtils.isNotBlank(request.getCustomerCode())) {
            AssertUtils.isTrue(request.getCustomerCode().length() > 50, "OC-01-001-01-15-004", "售达方编码", request.getCustomerCode().length(), "50");
        }
        if (StringUtils.isNotBlank(request.getShipToPartyCode())) {
            AssertUtils.isTrue(request.getShipToPartyCode().length() > 50, "OC-01-001-01-15-004", "送达方编码", request.getShipToPartyCode().length(), "50");
        }
        if (StringUtils.isNotBlank(request.getCustomerGrade())) {
            AssertUtils.isTrue(request.getCustomerGrade().length() > 50, "OC-01-001-01-15-004", "客户等级", request.getCustomerGrade().length(), "50");
        }
        if (StringUtils.isNotBlank(request.getCustomerGroup())) {
            AssertUtils.isTrue(request.getCustomerGroup().length() > 50, "OC-01-001-01-15-004", "客户组", request.getCustomerGroup().length(), "50");
        }
        if (StringUtils.isNotBlank(request.getCreatorCode())) {
            AssertUtils.isTrue(request.getCreatorCode().length() > 50, "OC-01-001-01-15-004", "创建人工号", request.getCreatorCode().length(), "50");
        }
        if (StringUtils.isNotBlank(request.getCustomerPurchaseOrderNo())) {
            AssertUtils.isTrue(request.getCustomerPurchaseOrderNo().length() > 50, "OC-01-001-01-15-004", "客户采购订单号", request.getCustomerPurchaseOrderNo().length(), "50");
        }
        if (StringUtils.isNotBlank(request.getNote())) {
            AssertUtils.isTrue(request.getNote().length() > 255, "OC-01-001-01-15-004", "备注（主单）", request.getNote().length(), "255");
        }
        if (StringUtils.isNotBlank(request.getDeliveryFactoryCode())) {
            AssertUtils.isTrue(request.getDeliveryFactoryCode().length() > 50, "OC-01-001-01-15-004", "发货工厂编码", request.getDeliveryFactoryCode().length(), "50");
        }
        if (StringUtils.isNotBlank(request.getStockCode())) {
            AssertUtils.isTrue(request.getStockCode().length() > 50, "OC-01-001-01-15-004", "发货库存地点编码", request.getStockCode().length(), "50");
        }
        if (StringUtils.isNotBlank(request.getDetailNote())) {
            AssertUtils.isTrue(request.getDetailNote().length() > 255, "OC-01-001-01-15-004", "订单行备注", request.getDetailNote().length(), "255");
        }
        if (StringUtils.isNotBlank(request.getModifierCode())) {
            AssertUtils.isTrue(request.getModifierCode().length() > 50, "OC-01-001-01-15-004", "修改人工号", request.getModifierCode().length(), "50");
        }
        if (StringUtils.isNotBlank(request.getContact())) {
            AssertUtils.isTrue(request.getContact().length() > 50, "OC-01-001-01-15-004", "联系人", request.getContact().length(), "50");
        }
        if (StringUtils.isNotBlank(request.getContactPhone())) {
            AssertUtils.isTrue(request.getContactPhone().length() > 50, "OC-01-001-01-15-004", "联系电话", request.getContactPhone().length(), "50");
        }
        if (CollectionUtils.isNotEmpty(request.getBusinessUnitCodeList())) {
            AssertUtils.isTrue(request.getBusinessUnitCodeList().size() > 10, "OC-01-001-01-15-004", "业务单元编码集合", request.getBusinessUnitCodeList().size(), "10");
            checkItemLength(request.getBusinessUnitCodeList(), 50, "业务单元编码");
            request.getBusinessUnitCodeList().forEach(businessUnitCode ->
                    dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_UNIT_CODE.getCode(), businessUnitCode, new FunctionException("OC-01-001-01-15-006", "业务单元编码集合")));
        }
        if (StringUtils.isNotBlank(request.getBusinessUnitCode())) {
            dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_UNIT_CODE.getCode(), request.getBusinessUnitCode(), new FunctionException("OC-01-001-01-15-006", "业务单元编码"));
        }
        if (CollectionUtils.isNotEmpty(request.getSalesDepartmentCodeList())) {
            AssertUtils.isTrue(request.getSalesDepartmentCodeList().size() > 10, "OC-01-001-01-15-004", "销售部门编码集合", request.getSalesDepartmentCodeList().size(), "10");
            checkItemLength(request.getSalesDepartmentCodeList(), 50, "销售部门编码");
        }
        if (CollectionUtils.isNotEmpty(request.getOrderChannelCodeList())) {
            AssertUtils.isTrue(request.getOrderChannelCodeList().size() > 10, "OC-01-001-01-15-004", "订单渠道编码集合", request.getOrderChannelCodeList().size(), "10");
            checkItemLength(request.getOrderChannelCodeList(), 50, "订单渠道编码");
            List<String> checkExistList = request.getOrderChannelCodeList().stream().filter(x -> !OrderChannelEnum.codes().contains(x)).collect(Collectors.toList());
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "订单渠道编码集合");
        }
        if (CollectionUtils.isNotEmpty(request.getSalesOrderNoList())) {
            AssertUtils.isTrue(request.getSalesOrderNoList().size() > 50, "OC-01-001-01-15-004", "销售订单号集合", request.getSalesOrderNoList().size(), "50");
            checkItemLength(request.getSalesOrderNoList(), 50, "销售订单号");
        }
        if (CollectionUtils.isNotEmpty(request.getPreOrderNoList())) {
            AssertUtils.isTrue(request.getPreOrderNoList().size() > 50, "OC-01-001-01-15-004", "预报订单号集合", request.getPreOrderNoList().size(), "50");
            checkItemLength(request.getPreOrderNoList(), 50, "预报订单号");
        }
        if (CollectionUtils.isNotEmpty(request.getSourceOrderNoList())) {
            AssertUtils.isTrue(request.getSourceOrderNoList().size() > 50, "OC-01-001-01-15-004", "来源单号集合", request.getSourceOrderNoList().size(), "50");
            checkItemLength(request.getSourceOrderNoList(), 50, "来源单号");
        }
        if (CollectionUtils.isNotEmpty(request.getSalesOrderTypeList())) {
            AssertUtils.isTrue(request.getSalesOrderTypeList().size() > 10, "OC-01-001-01-15-004", "订单类型编码集合", request.getSalesOrderTypeList().size(), "10");
            checkItemLength(request.getSalesOrderTypeList(), 50, "订单类型编码");
            List<String> checkExistList = null;
            if (OrderCategoryEnum.GROUP.getCode().equals(orderCategoryCode)) {
                checkExistList = request.getSalesOrderTypeList().stream().filter(x ->
                        !SalesOrderTypeEnum.groupOrderCodes().contains(x)).collect(Collectors.toList());
            } else {
                checkExistList = request.getSalesOrderTypeList().stream().filter(x ->
                        !SalesOrderTypeEnum.companyOrderCodes().contains(x)).collect(Collectors.toList());
            }
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "订单类型编码集合");
        }
        if (CollectionUtils.isNotEmpty(request.getSalesDepartmentCodeList())) {
            AssertUtils.isTrue(request.getSalesDepartmentCodeList().size() > 10, "OC-01-001-01-15-004", "销售部门编码集合", request.getSalesDepartmentCodeList().size(), "10");
            checkItemLength(request.getSalesDepartmentCodeList(), 50, "销售部门编码");
        }
        if (CollectionUtils.isNotEmpty(request.getSalesChannelCodeList())) {
            AssertUtils.isTrue(request.getSalesChannelCodeList().size() > 10, "OC-01-001-01-15-004", "销售渠道编码集合", request.getSalesChannelCodeList().size(), "10");
            checkItemLength(request.getSalesChannelCodeList(), 50, "销售渠道编码");

        }
        if (CollectionUtils.isNotEmpty(request.getReceiveStatusList())) {
            AssertUtils.isTrue(request.getReceiveStatusList().size() > 10, "OC-01-001-01-15-004", "单据收货状态集合", request.getReceiveStatusList().size(), "10");
            checkItemLength(request.getReceiveStatusList(), 10, "单据收货状态");
            List<String> statusList = SalesOrderGroupReceiveStatusEnum.codes().stream().map(String::valueOf).collect(Collectors.toList());
            List<String> checkExistList = request.getReceiveStatusList().stream().filter(x -> !statusList.contains(x)).collect(Collectors.toList());
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "单据收货状态集合");
        }
        if (CollectionUtils.isNotEmpty(request.getDetailReceiveStatusList())) {
            AssertUtils.isTrue(request.getDetailReceiveStatusList().size() > 10, "OC-01-001-01-15-004", "子单收货状态集合", request.getDetailReceiveStatusList().size(), "10");
            checkItemLength(request.getDetailReceiveStatusList(), 10, "子单收货状态");
            List<String> statusList = SalesOrderGroupReceiveStatusEnum.codes().stream().map(String::valueOf).collect(Collectors.toList());
            List<String> checkExistList = request.getDetailReceiveStatusList().stream().filter(x -> !statusList.contains(x)).collect(Collectors.toList());
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "子单收货状态集合");
        }
        if (CollectionUtils.isNotEmpty(request.getStatusList())) {
            AssertUtils.isTrue(request.getStatusList().size() > 10, "OC-01-001-01-15-004", "订单状态集合", request.getStatusList().size(), "10");
            checkItemLength(request.getStatusList(), 10, "订单状态");
            List<String> statusList = SalesOrderGroupStatusEnum.codes().stream().map(String::valueOf).collect(Collectors.toList());
            List<String> checkExistList = request.getStatusList().stream().filter(x -> !statusList.contains(x)).collect(Collectors.toList());
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "订单状态集合");
        }
        if (CollectionUtils.isNotEmpty(request.getDeliveryMethodCodeList())) {
            AssertUtils.isTrue(request.getDeliveryMethodCodeList().size() > 10, "OC-01-001-01-15-004", "配送方式编码集合", request.getDeliveryMethodCodeList().size(), "10");
            checkItemLength(request.getDeliveryMethodCodeList(), 50, "配送方式编码");
            List<String> checkExistList = request.getDeliveryMethodCodeList().stream().filter(x -> !SalesOrderDeliveryMethodEnum.codes().contains(x)).collect(Collectors.toList());
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "配送方式编码集合");
        }
        if (CollectionUtils.isNotEmpty(request.getOrderReasonCodeList())) {
            AssertUtils.isTrue(request.getOrderReasonCodeList().size() > 10, "OC-01-001-01-15-004", "订单原因编码集合", request.getOrderReasonCodeList().size(), "10");
            checkItemLength(request.getOrderReasonCodeList(), 50, "订单原因编码");
            List<String> checkExistList = request.getOrderReasonCodeList().stream().filter(x -> !SalesOrderReasonEnum.codes().contains(x)).collect(Collectors.toList());
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "订单原因编码集合");
        }
        if (CollectionUtils.isNotEmpty(request.getReceiveWarehouseCodeList())) {
            AssertUtils.isTrue(request.getReceiveWarehouseCodeList().size() > 10, "OC-01-001-01-15-004", "收货仓（逻辑仓库编码）集合", request.getReceiveWarehouseCodeList().size(), "10");
            checkItemLength(request.getReceiveWarehouseCodeList(), 50, "收货仓（逻辑仓库编码）");
        }
        if (CollectionUtils.isNotEmpty(request.getShippingWarehouseCodeList())) {
            AssertUtils.isTrue(request.getShippingWarehouseCodeList().size() > 10, "OC-01-001-01-15-004", "发货仓（逻辑仓库编码）集合", request.getShippingWarehouseCodeList().size(), "10");
            checkItemLength(request.getShippingWarehouseCodeList(), 50, "发货仓（逻辑仓库编码）");
        }
        if (CollectionUtils.isNotEmpty(request.getProductCodeList())) {
            AssertUtils.isTrue(request.getProductCodeList().size() > 10, "OC-01-001-01-15-004", "物料编码集合", request.getProductCodeList().size(), "10");
            checkItemLength(request.getProductCodeList(), 50, "物料编码");
        }
        if (CollectionUtils.isNotEmpty(request.getCloseReasonCodeList())) {
            AssertUtils.isTrue(request.getCloseReasonCodeList().size() > 10, "OC-01-001-01-15-004", "关闭原因编码集合", request.getCloseReasonCodeList().size(), "10");
            checkItemLength(request.getCloseReasonCodeList(), 50, "关闭原因编码");
            List<String> checkExistList = request.getCloseReasonCodeList().stream().filter(x -> !SalesOrderCloseEnum.codes().contains(x)).collect(Collectors.toList());
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "关闭原因编码集合");
        }
        if (CollectionUtils.isNotEmpty(request.getDetailStatusList())) {
            AssertUtils.isTrue(request.getDetailStatusList().size() > 10, "OC-01-001-01-15-004", "明细行状态集合", request.getDetailStatusList().size(), "10");
            checkItemLength(request.getDetailStatusList(), 10, "明细行状态");
            List<String> detailStatusList = SalesOrderGroupStatusEnum.codes().stream().map(String::valueOf).collect(Collectors.toList());
            List<String> checkExistList = request.getDetailStatusList().stream().filter(x -> !detailStatusList.contains(x)).collect(Collectors.toList());
            AssertUtils.isTrue(CollectionUtils.isNotEmpty(checkExistList), "OC-01-001-01-15-006", "明细行状态集合");
        }

        if (StringUtils.isNotEmpty(request.getOrderBizTag())) {
            AssertUtils.isTrue(request.getOrderBizTag().length() > 50, "OC-01-001-01-15-004", "订单标签", request.getOrderBizTag().length(), "50");
            AssertUtils.isTrue(!OrderBizTagEnum.codes().contains(request.getOrderBizTag()), "OC-01-001-01-15-006", "订单标签");
        }
        AssertUtils.isWholeNumber(request.getContactPhone(), false, "OC-01-001-01-15-006", "联系电话");

        // 页码、页数合法性校验
        AssertUtils.isTrue(request.getPage() > MnOrderCenterConstants.MAX_PAGE().intValue(), "OC-01-001-01-15-009", "页数", "最大" + MnOrderCenterConstants.MAX_PAGE().intValue());
        AssertUtils.isTrue(request.getSize() > MnOrderCenterConstants.MAX_SIZE().intValue(), "OC-01-001-01-15-009", "分页条数", "最大" + MnOrderCenterConstants.MAX_SIZE().intValue());
        AssertUtils.isTrue(request.getPage() <= 0, "OC-01-001-01-15-009", "页数", "最小值为1");
        AssertUtils.isTrue(request.getSize() <= 0, "OC-01-001-01-15-009", "分页条数", "最小值为1");

        // 字典值校验
        dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_TYPE.getCode(), request.getBusinessType(), new FunctionException("OC-01-001-01-15-006", "业态编码"));
        if (CollectionUtils.isNotEmpty(request.getFromSysList())) {
            AssertUtils.isTrue(request.getFromSysList().size() > 10, "OC-01-001-01-15-004", "来源系统编码集合", request.getFromSysList().size(), "10");
            checkItemLength(request.getFromSysList(), 50, "来源系统编码");
            request.getFromSysList().forEach(fromSys -> {
                dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.FROM_SYS.getCode(), fromSys, new FunctionException("OC-01-001-01-15-006", "来源系统编码"));
            });
        }
        if (StringUtils.isNotBlank(request.getFromSys())) {
            dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.FROM_SYS.getCode(), request.getFromSys(), new FunctionException("OC-01-001-01-15-006", "来源系统编码"));
        }
        if (StringUtils.isNotBlank(request.getFulfillmentTypeCode())) {
            AssertUtils.isTrue(request.getFulfillmentTypeCode().length() > 50, "OC-01-001-01-15-004", "履约方式", request.getFulfillmentTypeCode().length(), "50");
            // 履约方式枚举校验
            if (OrderCategoryEnum.GROUP.getCode().equals(orderCategoryCode)) {
                List<DictionaryResponse> fulfillmentTypeList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.GROUP_SALE_FULFILLMENT_TYPE.getCode());
                Map<String, DictionaryResponse> fulfillmentTypeMap = fulfillmentTypeList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
                DictionaryResponse dictionaryResponse = fulfillmentTypeMap.get(request.getFulfillmentTypeCode());
                AssertUtils.isTrue(dictionaryResponse == null, "OC-01-001-01-15-006", "履约方式");
            } else {
                List<DictionaryResponse> fulfillmentTypeList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.COMPANY_SALE_FULFILLMENT_TYPE.getCode());
                Map<String, DictionaryResponse> fulfillmentTypeMap = fulfillmentTypeList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
                AssertUtils.isTrue(fulfillmentTypeMap.get(request.getFulfillmentTypeCode()) == null, "OC-01-001-01-15-006", "履约方式");
            }
        }
        if (StringUtils.isNotBlank(request.getFulfillmentPriority())) {
            AssertUtils.isTrue(request.getFulfillmentPriority().length() > 10, "OC-01-001-01-15-004", "履约优先级", request.getFulfillmentPriority().length(), "10");
            // 履约优先级枚举校验 0 100
            FulfillmentPriorityEnum fulfillmentPriorityEnum = FulfillmentPriorityEnum.of(request.getFulfillmentPriority());
            AssertUtils.isTrue(fulfillmentPriorityEnum == null, "OC-01-001-01-15-006", "履约优先级");
        }
        // 是否关闭合法校验
        if (StringUtils.isNotBlank(request.getIsClose())) {
            AssertUtils.isTrue(request.getIsClose().length() > 50, "OC-01-001-01-15-004", "是否关闭", request.getIsClose().length(), "50");
            AssertUtils.isTrue(!Lists.newArrayList(GeneralStatusEnum.YES.getCode().toString(), GeneralStatusEnum.NO.getCode().toString()).contains(request.getIsClose()), "OC-01-001-01-15-006", "是否关闭");
        }

        // 时间格式确认
//        Map<String, String> vaildTimeMap = timeFormatCheck(request.getVaildBeginTime(), request.getValidEndTime(), "生效开始日期（开始日期）", "生效开始日期（结束日期）", TYPE_DATE);
        Map<String, String> expectedDeliveryTimeMap = AssertUtils.timeFormatCheck(request.getExpectedDeliveryBeginDate(), request.getExpectedDeliveryEndDate(), "期望交货日期（开始日期）", "期望交货日期（结束日期）", AssertUtils.TYPE_DATE);
        Map<String, String> createTimeMap = AssertUtils.timeFormatCheck(request.getCreateBeginTime(), request.getCreateEndTime(), "创建时间（开始时间）", "创建时间（结束时间）", AssertUtils.TYPE_TIME);
        Map<String, String> updateTimeMap = AssertUtils.timeFormatCheck(request.getModifyBeginTime(), request.getModifyEndTime(), "修改时间（开始时间）", "修改时间（结束时间）", AssertUtils.TYPE_TIME);
        Map<String, String> sourceTime = AssertUtils.timeFormatCheck(request.getSourceBeginTime(), request.getSourceEndTime(), "来单时间（开始时间）", "来单时间（结束时间）", AssertUtils.TYPE_TIME);
//        request.setVaildBeginTime(vaildTimeMap.get("startTime"));
//        request.setValidEndTime(vaildTimeMap.get("endTime"));
        request.setExpectedDeliveryBeginDate(expectedDeliveryTimeMap.get(AssertUtils.START_TIME));
        request.setExpectedDeliveryEndDate(expectedDeliveryTimeMap.get(AssertUtils.END_TIME));
        request.setCreateBeginTime(createTimeMap.get(AssertUtils.START_TIME));
        request.setCreateEndTime(createTimeMap.get(AssertUtils.END_TIME));
        request.setModifyBeginTime(updateTimeMap.get(AssertUtils.START_TIME));
        request.setModifyEndTime(updateTimeMap.get(AssertUtils.END_TIME));
        request.setSourceBeginTime(sourceTime.get(AssertUtils.START_TIME));
        request.setSourceEndTime(sourceTime.get(AssertUtils.END_TIME));
    }

    private void checkItemLength(List<String> items, Integer maxLength, String errMsg) {
        items.forEach(x -> {
            AssertUtils.lengthCheck(x, maxLength, false, "OC-01-001-01-15-004", errMsg);
            // AssertUtils.isTrue(x.length() > maxLength, "OC-01-001-01-15-004", errMsg, x.length(), maxLength);
        });
    }

    private void convertRequest(SalesOrderAtomPageQueryRequest salesOrderAtomPageQueryRequest) {
        List<String> bisUnitCodes = Lists.newArrayList(salesOrderAtomPageQueryRequest.getBusinessUnitCode());
        Map<String, String> organizationMap = baseDataServiceAdapter.baselineGetOrganizationByCodes2(bisUnitCodes);
        if (MapUtils.isEmpty(organizationMap)) {
            throw new FunctionException("OC-01-001-01-16-011", salesOrderAtomPageQueryRequest.getBusinessUnitCode());
        }
        AssertUtils.isTrue(salesOrderAtomPageQueryRequest.getSize() > 100, "OC-01-001-01-15-009", "分页条数", "最大100");

        String orgId = organizationMap.get(salesOrderAtomPageQueryRequest.getBusinessUnitCode());
        if (StringUtils.isBlank(orgId)) {
            throw new FunctionException("OC-01-001-01-16-011", salesOrderAtomPageQueryRequest.getBusinessUnitCode());
        }
        salesOrderAtomPageQueryRequest.setBusinessUnitCode(orgId);
    }

    private PageResult<List<SalesOrderAtomQueryResponse>> pageQueryByEs(SalesOrderAtomPageQueryRequest pageRequest, String orderTag) {
        StopWatch stopWatch = new StopWatch("销售订单批量查询");

        stopWatch.start("buildPageQueryOrderEsListRequest");
        PageQueryOrderEsListRequest4OrderCenter request = buildPageQueryOrderEsListRequest(pageRequest);
        stopWatch.stop();

        if (StringUtils.isNotBlank(orderTag)) {
            request.setOrderTag(orderTag);
        }

        stopWatch.start("pageQueryOrderList4OrderCenter");
        request.setBusinessUnitOutCode(request.getBusinessUnitCode());
        request.setBusinessUnitCode(null);
        Result<List<ObtcSaleOrderIndex>> listResult = obtcSaleOrderIndexService.pageQueryOrderList4OrderCenter(request);
        stopWatch.stop();

        stopWatch.start("convertMainOrder");
        List<SalesOrderAtomQueryResponse> response = SaleOrderIndexToSalesOrderAtomQueryResponseConvertor.convertMainOrder(listResult);
        stopWatch.stop();

        log.info("SalesOrderAtomQueryServiceImpl#pageQueryByEs stopWatch.getTotalTimeSeconds()={}", stopWatch.getTotalTimeSeconds());
        log.info("SalesOrderAtomQueryServiceImpl#pageQueryByEs stopWatch.prettyPrint()={}", stopWatch.prettyPrint(TimeUnit.MILLISECONDS));

        return PageResult.of(response, listResult.getTotal());
    }

    @NotNull
    private PageQueryOrderEsListRequest4OrderCenter buildPageQueryOrderEsListRequest(SalesOrderAtomPageQueryRequest pageRequest) {
        PageQueryOrderEsListRequest4OrderCenter esRequest = new PageQueryOrderEsListRequest4OrderCenter();
        esRequest.setDir("main_gmt_create");
        String fromSys = pageRequest.getFromSys();
        String businessType = pageRequest.getBusinessType();
        String businessUnitCode = pageRequest.getBusinessUnitCode();
        String createTimeStart = pageRequest.getCreateBeginTime();
        String createTimeEnd = pageRequest.getCreateEndTime();
        List<String> salesOrderType = pageRequest.getSalesOrderType();
        String customerCode = pageRequest.getCustomerCode();
        String shipToPartyCode = pageRequest.getShipToPartyCode();
        String salesOrganizationCode = pageRequest.getSalesOrganizationCode();
        List<String> salesChannelCode = pageRequest.getSalesChannelCode();
        List<String> salesDepartmentCode = pageRequest.getSalesDepartmentCode();
        String productGroupCode = pageRequest.getProductGroupCode();
        List<String> businessChannel = pageRequest.getBusinessChannel();
        String relationNo = pageRequest.getRelationNo();

        //一盘货改造
        List<String> fromSysList = pageRequest.getFromSysList();
        List<String> businessUnitCodeList = pageRequest.getBusinessUnitCodeList();
        String preOrderNo = pageRequest.getPreOrderNo();
        List<String> sourceOrderNoList = pageRequest.getSourceOrderNoList();
        String businessChannelCode = pageRequest.getBusinessChannelCode();
        List<String> statusList = pageRequest.getStatusList();
        String expectedDeliveryBeginTime = pageRequest.getExpectedDeliveryBeginDate();
        String expectedDeliveryEndTime = pageRequest.getExpectedDeliveryEndDate();
        String updateBeginTime = pageRequest.getModifyBeginTime();
        String updateEndTime = pageRequest.getModifyEndTime();
        List<String> deliveryMethodCodeList = pageRequest.getDeliveryMethodCodeList();
        String note = pageRequest.getNote();
        String customerPurchaseOrderNo = pageRequest.getCustomerPurchaseOrderNo();
        List<String> orderReasonCodeList = pageRequest.getOrderReasonCodeList();
        List<String> receiveWarehouseCodeList = pageRequest.getReceiveWarehouseCodeList();
        String deliveryFactoryCode = pageRequest.getDeliveryFactoryCode();
        String stockCode = pageRequest.getStockCode();
        List<String> shippingWarehouseCodeList = pageRequest.getShippingWarehouseCodeList();
        String contact = pageRequest.getContact();
        String contactPhone = pageRequest.getContactPhone();
        List<String> closeReasonCodeList = pageRequest.getCloseReasonCodeList();
        List<String> productCodeList = pageRequest.getProductCodeList();
        List<String> orderChannelCodeList = pageRequest.getOrderChannelCodeList();
        String salesOrderNo = pageRequest.getSalesOrderNo();
        String customerGrade = pageRequest.getCustomerGrade();
        String customerGroup = pageRequest.getCustomerGroup();
        String fulfillmentTypeCode = pageRequest.getFulfillmentTypeCode();
        String fulfillmentPriority = pageRequest.getFulfillmentPriority();
        String orderTag = pageRequest.getOrderTag();
        String receiveStatus = pageRequest.getReceiveStatus();
        String creatorCode = pageRequest.getCreatorCode();
        List<String> outOrderNoList = pageRequest.getOutOrderNoList();
        List<String> kaSystemList = pageRequest.getKaSystemList();
        List<String> kaAreaList = pageRequest.getKaAreaList();
        String sourceBeginTime = pageRequest.getSourceBeginTime();
        String sourceEndTime = pageRequest.getSourceEndTime();

        if (StringUtils.isNotBlank(pageRequest.getModifierCode())) {
            esRequest.setModifierCode(pageRequest.getModifierCode());
        }
        if (StringUtils.isNotBlank(pageRequest.getOrderCategoryCode())) {
            esRequest.setOrderCategoryCode(pageRequest.getOrderCategoryCode());
        }
        if (CollectionUtils.isNotEmpty(pageRequest.getOrderCategoryCodeList())) {
            esRequest.setOrderCategoryCodeList(pageRequest.getOrderCategoryCodeList());
        }
        if (CollectionUtils.isNotEmpty(pageRequest.getSalesOrderNoList())) {
            esRequest.setBizCodeList(pageRequest.getSalesOrderNoList());
        }
        if (CollectionUtils.isNotEmpty(pageRequest.getPreOrderNoList())) {
            esRequest.setPreOrderNoList(pageRequest.getPreOrderNoList());
        }
        if (CollectionUtils.isNotEmpty(pageRequest.getReceiveStatusList())) {
            esRequest.setSubStatusList(pageRequest.getReceiveStatusList());
        }
        if (StringUtils.isNotBlank(pageRequest.getOrderTag())) {
            esRequest.setOrderTag(pageRequest.getOrderTag());
        }
        if (StringUtils.isNotBlank(pageRequest.getOrderBizTag())) {
            esRequest.setOrderBizTag(pageRequest.getOrderBizTag());
        }
        if (StringUtils.isNotBlank(fromSys)) {
            esRequest.setFromSys(fromSys);
        }
        if (StringUtils.isNotBlank(businessType)) {
            esRequest.setBusinessType(businessType);
        }
        if (StringUtils.isNotBlank(businessUnitCode)) {
            esRequest.setBusinessUnitCode(businessUnitCode);
        }
        try {
            if (StringUtils.isNotBlank(createTimeStart)) {
                esRequest.setOrderStartTime(DateUtils.formatString2Date(createTimeStart, "yyyy-MM-dd HH:mm:ss"));
            }
            if (StringUtils.isNotBlank(createTimeEnd)) {
                esRequest.setOrderEndTime(DateUtils.formatString2Date(createTimeEnd, "yyyy-MM-dd HH:mm:ss"));
            }
            if (StringUtils.isNotBlank(updateBeginTime)) {
                esRequest.setUpdateStartTime(DateUtils.formatString2Date(updateBeginTime, "yyyy-MM-dd HH:mm:ss"));
            }
            if (StringUtils.isNotBlank(updateEndTime)) {
                esRequest.setUpdateEndTime(DateUtils.formatString2Date(updateEndTime, "yyyy-MM-dd HH:mm:ss"));
            }
            if (StringUtils.isNotBlank(expectedDeliveryBeginTime)) {
                esRequest.setExpectedDeliveryBeginTime(DateUtils.formatString2Date(expectedDeliveryBeginTime, "yyyy-MM-dd"));
            }
            if (StringUtils.isNotBlank(expectedDeliveryEndTime)) {
                esRequest.setExpectedDeliveryEndTime(DateUtils.formatString2Date(expectedDeliveryEndTime, "yyyy-MM-dd"));
            }
//            if (StringUtils.isNotBlank(vaildBeginTime)) {
//                esRequest.setVaildBeginTime(DateUtils.formatString2Date(vaildBeginTime, "yyyy-MM-dd"));
//            }
//            if (StringUtils.isNotBlank(validEndTime)) {
//                esRequest.setValidEndTime(DateUtils.formatString2Date(validEndTime, "yyyy-MM-dd"));
//            }
        } catch (Exception e) {
            log.error("ExchangeOrderAtomQueryServiceImpl#buildPageQueryOrderEsListRequest error", e);
            throw new FunctionException("OC-00-001-01-15-001", "日期");
        }
        if(StringUtils.isNotBlank(creatorCode)){
            esRequest.setCreatorCode(creatorCode);
        }
        if (CollectionUtils.isNotEmpty(salesOrderType)) {
            esRequest.setOrderTypeList(salesOrderType);
        }
        if (StringUtils.isNotBlank(customerCode)) {
            esRequest.setCustomerCode(customerCode);
        }
        if (StringUtils.isNotBlank(shipToPartyCode)) {
            esRequest.setAddressCode(shipToPartyCode);
        }
        if (StringUtils.isNotBlank(shipToPartyCode)) {
            esRequest.setAddressCode(shipToPartyCode);
        }
        if (StringUtils.isNotBlank(salesOrganizationCode)) {
            esRequest.setSalesOrganizationOutCode(salesOrganizationCode);
        }
        if (CollectionUtils.isNotEmpty(salesChannelCode)) {
            esRequest.setSaleChannelOutCode(salesChannelCode);
        }
        if (CollectionUtils.isNotEmpty(salesDepartmentCode)) {
            esRequest.setSaleDepartmentOutCode(salesDepartmentCode);
        }
        if (StringUtils.isNotBlank(productGroupCode)) {
            esRequest.setProductGroupCode(productGroupCode);
        }
        if (CollectionUtils.isNotEmpty(businessChannel)) {
            esRequest.setBuzChannel(businessChannel);
        }
        if (CollectionUtils.isNotEmpty(fromSysList)) {
            esRequest.setFromSysList(fromSysList);
        }
        if(CollectionUtils.isNotEmpty(businessUnitCodeList)){
            esRequest.setBusinessUnitCodeList(businessUnitCodeList);
        }
        if (StringUtils.isNotBlank(preOrderNo)) {
            esRequest.setPreOrderNo(preOrderNo);
        }
        if (CollectionUtils.isNotEmpty(sourceOrderNoList)) {
            esRequest.setSourceOrderNoList(sourceOrderNoList);
        }
        if(StringUtils.isNotBlank(productGroupCode)){
            esRequest.setProductGroupCode(productGroupCode);
        }
        if(StringUtils.isNotBlank(businessChannelCode)){
            esRequest.setBusinessChannelCode(businessChannelCode);
        }
        if(CollectionUtils.isNotEmpty(statusList)){
            esRequest.setMainStatusList(statusList);
        }
        if(CollectionUtils.isNotEmpty(deliveryMethodCodeList)){
            esRequest.setDeliveryMethodCodeList(deliveryMethodCodeList);
        }
        if(StringUtils.isNotBlank(note)){
            esRequest.setOrderRemark(note);
        }
        if(StringUtils.isNotBlank(customerPurchaseOrderNo)){
            esRequest.setCustomerPurchaseOrderNo(customerPurchaseOrderNo);
        }
        if(CollectionUtils.isNotEmpty(orderReasonCodeList)){
            esRequest.setOrderReasonCodeList(orderReasonCodeList);
        }
        if(CollectionUtils.isNotEmpty(receiveWarehouseCodeList)){
            esRequest.setReceiveWarehouseCodeList(receiveWarehouseCodeList);
        }
        if(StringUtils.isNotBlank(deliveryFactoryCode)){
            esRequest.setDeliveryFactoryCode(deliveryFactoryCode);
        }
        if(StringUtils.isNotBlank(stockCode)){
            esRequest.setStockCode(stockCode);
        }
        if(CollectionUtils.isNotEmpty(shippingWarehouseCodeList)){
            esRequest.setShippingWarehouseCodeList(shippingWarehouseCodeList);
        }
        if(StringUtils.isNotBlank(contact)){
            esRequest.setContactor(contact);
        }
        if(StringUtils.isNotBlank(contactPhone)){
            esRequest.setPhone(contactPhone);
        }
        if(CollectionUtils.isNotEmpty(closeReasonCodeList)){
            esRequest.setCloseReasonCodeList(closeReasonCodeList);
        }
        if(CollectionUtils.isNotEmpty(productCodeList)){
            esRequest.setProductCodeList(productCodeList);
        }
        if(CollectionUtils.isNotEmpty(orderChannelCodeList)){
            esRequest.setOrderChannelCodeList(orderChannelCodeList);
        }
        if(StringUtils.isNotBlank(salesOrderNo)){
            esRequest.setOrderBizId(salesOrderNo);
        }
        if(StringUtils.isNotBlank(customerGrade)){
            esRequest.setCustomerGrade(customerGrade);
        }
        if(StringUtils.isNotBlank(customerGroup)){
            esRequest.setCustomerGroup(customerGroup);
        }
        if(StringUtils.isNotBlank(fulfillmentTypeCode)){
            esRequest.setFulfillmentTypeCode(fulfillmentTypeCode);
        }
        if(StringUtils.isNotBlank(fulfillmentPriority)){
            esRequest.setFulfillmentPriority(fulfillmentPriority);
        }
        if(StringUtils.isNotBlank(orderTag)){
            esRequest.setOrderTag(orderTag);
        }
        if(CollectionUtils.isNotEmpty(outOrderNoList)){
            esRequest.setOuterIdList(outOrderNoList);
        }
        if(CollectionUtils.isNotEmpty(kaSystemList)){
            esRequest.setKaSystemList(kaSystemList);
        }
        if(CollectionUtils.isNotEmpty(kaAreaList)){
            esRequest.setKaAreaList(kaAreaList);
        }
        if(StringUtils.isNotBlank(sourceBeginTime)){
            esRequest.setSourceBeginTime(sourceBeginTime);
        }
        if(StringUtils.isNotBlank(sourceEndTime)){
            esRequest.setSourceEndTime(sourceEndTime);
        }
        esRequest.setPage(pageRequest.getPage());
        esRequest.setSize(pageRequest.getSize());
        esRequest.setLimit(pageRequest.getSize());
        if (StringUtils.isNotBlank(relationNo)) {
            esRequest.setRelationNo(relationNo);
        }

        return esRequest;
    }

    private void baseParamsCheck(String fromSys, String businessUnitCode, String businessType, int page, int size) {
        // 业务单元编码长度、合法性校验
        if (StringUtils.isNotBlank(businessUnitCode)) {
            AssertUtils.isTrue(StringUtils.isBlank(businessUnitCode), "OC-01-001-01-15-001", "业务单元编码");
            AssertUtils.lengthCheck(businessUnitCode, 50, false, "OC-01-001-01-15-004", "业务单元编码");
            dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_UNIT_CODE.getCode(), businessUnitCode, new FunctionException("OC-01-001-01-15-006", "业务单元编码"));
        }
        // 来源系统长度、合法性校验
        if (StringUtils.isNotBlank(fromSys)) {
            AssertUtils.lengthCheck(fromSys, 50, false, "OC-01-001-01-15-004", "来源系统编码");
            dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.FROM_SYS.getCode(), fromSys, new FunctionException("OC-01-001-01-15-006", "来源系统编码"));
        }
        // 业态编码长度、合法性校验
        AssertUtils.isTrue(StringUtils.isBlank(businessType), "OC-01-001-01-15-001", "业态编码");
        AssertUtils.lengthCheck(businessType, 10, true, "OC-01-001-01-15-004", "业态编码");
        dictionaryVerifyAdapter.verifyDictionary(DictionaryTypeEnum.BUSINESS_TYPE.getCode(), businessType, new FunctionException("OC-01-001-01-15-006", "业态编码"));

        AssertUtils.isTrue(page > MnOrderCenterConstants.MAX_PAGE().intValue(), "OC-01-001-01-15-009", "页数", "最大" + MnOrderCenterConstants.MAX_PAGE().intValue());
        AssertUtils.isTrue(size > MnOrderCenterConstants.MAX_SIZE().intValue(), "OC-01-001-01-15-009", "分页条数", "最大" + MnOrderCenterConstants.MAX_SIZE().intValue());
        AssertUtils.isTrue(page <= 0, "OC-01-001-01-15-009", "页数", "最小值为1");
        AssertUtils.isTrue(size <= 0, "OC-01-001-01-15-009", "分页条数", "最小值为1");
    }

    private void paramsCheck(SalesOrderAtomQueryListRequest request, String orderCategory) {
        baseParamsCheck(request.getFromSys(), request.getBusinessUnitCode(), request.getBusinessType(), request.getPage(), request.getSize());
        AssertUtils.isTrue(request.getSize() > 100, "OC-01-001-01-15-009", "分页条数", "最大100");

        if (OrderCategoryEnum.GROUP.getCode().equals(orderCategory)) {
            AssertUtils.isTrue(CollectionUtils.isEmpty(request.getSourceOrderNo()) && CollectionUtils.isEmpty(request.getSalesOrderNo()),
                    "OC-01-001-01-15-014","销售订单单号", "来源单号");
            // 来源单号
            if (CollectionUtils.isNotEmpty(request.getSourceOrderNo())) {
                request.getSourceOrderNo().forEach(x -> {
                    AssertUtils.lengthCheck(x, 50, false, "OC-01-001-01-15-004", "来源单号");
                });
            }
            // 销售单号
            if (CollectionUtils.isNotEmpty(request.getSalesOrderNo())) {
                request.getSalesOrderNo().forEach(x -> {
                    AssertUtils.lengthCheck(x, 50, false, "OC-01-001-01-15-004", "销售订单单号");
                });
            }
        } else {
            List<String> sourceOrderNo = request.getSourceOrderNo();
            List<String> salesOrderNo = request.getSalesOrderNo();
            List<String> preOrderNo = request.getPreOrderNo();
            List<String> outOrderNo = request.getOutOrderNo();
            List<String> deliveryOrderNo = request.getDeliveryOrderNo();
            // 多个单号取交集
            if (CollectionUtils.isEmpty(sourceOrderNo) && CollectionUtils.isEmpty(salesOrderNo) && CollectionUtils.isEmpty(preOrderNo)
                    && CollectionUtils.isEmpty(outOrderNo) && CollectionUtils.isEmpty(deliveryOrderNo)) {
                throw new FunctionException("OC-01-001-01-15-001", "销售订单单号、预报订单号、外部单号、交货单号");
            }
        }
    }

    private void filterBlankOrderNo(SalesOrderAtomQueryListRequest salesOrderAtomQueryListRequest) {

        List<String> sourceOrderNo = salesOrderAtomQueryListRequest.getSourceOrderNo();
        List<String> salesOrderNo = salesOrderAtomQueryListRequest.getSalesOrderNo();
        List<String> preOrderNo = salesOrderAtomQueryListRequest.getPreOrderNo();
        List<String> outOrderNo = salesOrderAtomQueryListRequest.getOutOrderNo();
        List<String> deliveryOrderNo = salesOrderAtomQueryListRequest.getDeliveryOrderNo();

        sourceOrderNo = Optional.ofNullable(sourceOrderNo).orElse(Lists.newArrayList()).stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
        salesOrderNo = Optional.ofNullable(salesOrderNo).orElse(Lists.newArrayList()).stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
        preOrderNo = Optional.ofNullable(preOrderNo).orElse(Lists.newArrayList()).stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
        outOrderNo = Optional.ofNullable(outOrderNo).orElse(Lists.newArrayList()).stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
        deliveryOrderNo = Optional.ofNullable(deliveryOrderNo).orElse(Lists.newArrayList()).stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());

        salesOrderAtomQueryListRequest.setSourceOrderNo(sourceOrderNo);
        salesOrderAtomQueryListRequest.setSalesOrderNo(salesOrderNo);
        salesOrderAtomQueryListRequest.setPreOrderNo(preOrderNo);
        salesOrderAtomQueryListRequest.setOutOrderNo(outOrderNo);
        salesOrderAtomQueryListRequest.setDeliveryOrderNo(deliveryOrderNo);
    }

}
